var serverIP = "10.8.37.218";

var script = document.createElement('script');
script.src = 'jquery.mobile-1.3.0.min.js';
script.type = 'text/javascript';
document.getElementsByTagName('head')[0].appendChild(script);

function callCarInfo(){
      
      carInfo();
      setInterval(function(){carInfo();},5000);

   }

   var sozluk = new Object({
          'state' : '<strong>Durum:</strong>',
          'freeSeats' : '<strong>BoşKoltuk Sayısı:</strong>',
          'battery' : '<strong>Pil:</strong>',
          'estimatedTime' : '<strong>Tahmini Varış Süresi:</strong>',
          'estimatedDistance' : '<strong>Tahmini Mesafe:</strong>',
          'speed' : '<strong>Hız:</strong>',
          'location' : '<strong>Lokasyon:</strong>',
          'totalDistance' : '<strong>Gittiği Toplam Mesafe:</strong>',
          'destination' : '<strong>Varış Yeri:</strong>'
        });

   var selectedVehicleId;

   function carInfo() {

      selectedVehicleId = document.getElementById ( "selectCar" ).value;
         var obj;
         var xmlhttp = new XMLHttpRequest();
          
         var url = "http://"+serverIP+"/prtCar11/php/Vehicles.php/" + selectedVehicleId;

          xmlhttp.open("GET",url,false);
          xmlhttp.send();
          var json = xmlhttp.responseText; 
          obj = JSON.parse(json);
      
        	  
          for(var count = 0; count< obj.length; count++){
              document.getElementById("cinfo").innerHTML = sozluk['state'] + " "+ obj[count].status + "<br/>" +
        			  sozluk['freeSeats'] + " "+ obj[count].freeSeats + "<br/>" +
        			  sozluk['battery'] + " "+ obj[count].battery + "<br/>" +
        			  sozluk['speed'] + " "+ obj[count].speed + "<br/>" +
        			  sozluk['location'] + " "+ obj[count].position + "<br/>" +
                	  sozluk['destination'] +" "+ obj[count].destination + "<br/>" +
        			  sozluk['totalDistance'] + " "+ obj[count].distanceTravelled + "<br/>";         
       }
   }

    function prt_graph(){
          
          var selectedGraphElement = document.getElementById ( "carGraph" ).value;
          var url = "http://"+serverIP+"/prtCar11/php/VehicleInfo.php/" + selectedVehicleId + "/currentTime/" + selectedGraphElement;

          if(selectedGraphElement != "")
          {
            var xmlhttp = new XMLHttpRequest();
            xmlhttp.open("GET",url,false);
            xmlhttp.send();
            var json = xmlhttp.responseText; 
            obj = JSON.parse(json);
            graphObject = obj.data;
            xAx = obj.xAxis;
            yAx = obj.yAxis;
            drawGraph('#prt_flot', xAx, yAx, graphObject);
          }
          else
          {
            $('#prt_flot').empty();
          }
    }

	var selectedStation;

	function stationInfo(sId){    
	
		$.mobile.loading( 'show', {
		  text: 'Lütfen Bekleyiniz',
		  textVisible: true,
		  theme: 'b',
		  html: ""
		});

	  $('#sinfo').empty();
	
	  selectedStation = sId.value;
	
	  $.getJSON('http://'+serverIP+'/prtCar11/php/Stations.php/'+ selectedStation, function(result)
	  {
	    var stationInfo = result[0];
	    
	     if(stationInfo){
	      $.mobile.loading("hide");
	    }
	
	    $("#sinfo").append("<strong>Yolcu Sayısı: </strong>" + stationInfo.numberOfPassenger);
	
	  });

	}


    function sta_graph()
      {
        
        var selectedGraph = document.getElementById ( "sta_statistic" ).value;

        var url = "http://"+serverIP+"/prtCar11/php/Stations.php/" + selectedStation + "/" + selectedGraph;
		
        if(selectedGraph != "")
        {
          var graphObject;
          var xAx;
          var yAx;
          var xmlhttp;
                  
         xmlhttp=new XMLHttpRequest();         
		
          xmlhttp.open('GET',url,false);
          xmlhttp.send();
          var json = xmlhttp.responseText;
          obj = JSON.parse(json);
          graphObject = obj.data;
          xAx = obj.xAxis;
          yAx = obj.yAxis;
          drawGraph('#sta_flot', xAx, yAx, graphObject);
        }
        else
        {
          $('#sta_flot').empty();
        }
      }

function drawGraph(whereToDraw, xAx, yAx, graphData)
{
    elem = $(whereToDraw);

    $.plot(
      elem,
      [{label: yAx + "/" + xAx,  data: graphData}],
        {
          lines: {show: true},
          points: {show: true},
          grid:
          {
            backgroundColor: { colors: ["#fff", "#eee"] }
          }
        }
    );
}




   function sendCommand(command,carId){
      
      var comm = "";
      var commandString="";

      if(command==1){
         comm="START";
      }
     else if(command==2){
         comm="STOP";
      }
      else{
         comm="CHARGE";
      }
      var path = "http://localhost:85/"+ carId + "/"+comm+"/";

      console.log("path: " + path);
      $.get(path,function(){
         alert("istek gonderildi");});

   }
   
   /*
    function onDeviceReady() {
        navigator.network.isReachable(serverIP, reachableCallback, {});
        alert(navigator.network.connection.type)
    }

      function reachableCallback(reachability) {
        // There is no consistency on the format of reachability
        var networkState = navigator.network.connection.type;

        var states = {};
        states[NetworkStatus.NOT_REACHABLE]                      = 'No network connection';
        states[NetworkStatus.REACHABLE_VIA_CARRIER_DATA_NETWORK] = 'Carrier data connection';
        states[NetworkStatus.REACHABLE_VIA_WIFI_NETWORK]         = 'WiFi connection';

        alert('Connection type: ' + states[networkState]);

        if(networkState == NetworkStatus.NOT_REACHABLE)
          console.log("Erdem sus len" + networkState );
          app.exitApp();
    }*/